package com.mj.Set;


import com.mj.Set.tree.BinaryTree;
import com.mj.Set.tree.RBTree;

import java.util.Comparator;

/**
 * @Author: LDeng
 * @Date: 2021-03-10 9:12
 */
public class TreeSet<E> implements Set<E> {
    private RBTree<E> tree;

    public TreeSet(){
        this(null);
    }

    public TreeSet(Comparator<E> comparator){
        tree=new RBTree<>(comparator);
    }
    @Override
    public int size() {
        return tree.size();
    }

    @Override
    public boolean isEmpty() {
        return tree.isEmpty();
    }

    @Override
    public void clear() {
        tree.clear();
    }

    @Override
    public boolean contains(E element) {
        return tree.contains(element);
    }

    @Override
    public void add(E element) {
        tree.add(element);//红黑树默认是去重的
    }

    @Override
    public void remove(E element) {
        tree.remove(element);
    }

    @Override
    public void traversal(Visitor visitor) {
        tree.inorderTraversal(new BinaryTree.Visitor<E>() {
            @Override
            public boolean visit(E element) {//注意红黑树要的visitor和set里面定义的visitor是不一样的对象
                return visitor.visit(element);
            }
        });
    }
}
